<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="empName">
        <el-input
          style="width: 160px;"
          v-model="queryParams.empName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加班内容" prop="otStyel">
        <el-input
          style="width: 160px;"
          v-model="queryParams.otStyel"
          placeholder="请输入加班内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始时间" prop="otStartTime">
        <el-date-picker clearable
                        style="width: 160px;"
                        v-model="queryParams.otStartTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择起始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="otEndTime">
        <el-date-picker clearable
                        style="width: 160px;"
                        v-model="queryParams.otEndTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['check:overtime:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['check:overtime:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['check:overtime:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          round
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['check:overtime:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="overtimeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="记录号" align="center" prop="otId"/>
      <el-table-column label="员工编号" align="center" prop="empId"/>
      <el-table-column label="员工姓名" align="center" prop="empName"/>
      <el-table-column label="加班内容" align="center" prop="otStyel"/>
      <el-table-column label="起始时间" align="center" prop="otStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.otStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="otEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.otEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="加班时长" align="center" prop="differTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.differTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:overtime:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['check:overtime:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加班管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工" prop="empId">
          <!--          下拉框-->
          <!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加班内容" prop="otStyel">
          <el-input v-model="form.otStyel" type="textarea" placeholder="请输入加班内容" :autosize="{ minRows: 4}"
                    maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="起始时间" prop="otStartTime">
          <el-date-picker clearable
                          v-model="form.otStartTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="otEndTime">
          <el-date-picker clearable
                          v-model="form.otEndTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="版本号" prop="version">-->
        <!--          <el-input v-model="form.version" placeholder="请输入版本号" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOvertime, getOvertime, delOvertime, addOvertime, updateOvertime, listEmployee} from "@/api/check/overtime";
import {delVacate} from "@/api/check/vacate";

export default {
  name: "Overtime",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加班管理表格数据
      overtimeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        otStyel: null,
        otStartTime: null,
        otEndTime: null,
        empName: null,
      },
      //下拉框数据
      employee: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        empId: [
          {required: true, message: "员工姓名不能为空", trigger: "blur"}
        ],
        otStyel: [
          {required: true, message: "加班内容不能为空", trigger: "blur"},
          {max: 99, message: "最大字符应小于100个", trigger: "change"}
        ],
        otStartTime: [
          {required: true, message: "起始时间不能为空", trigger: "blur"}
        ],
        otEndTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询加班管理列表 */
    getList() {
      this.loading = true;
      listOvertime(this.queryParams).then(response => {
        this.overtimeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        otId: null,
        empId: null,
        otStyel: null,
        otStartTime: null,
        otEndTime: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.otId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加加班管理";

      // 发送请求
      listEmployee().then(response => {
        this.employee = response.rows;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const otId = row.otId || this.ids
      getOvertime(otId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改加班管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.otId != null) {
            updateOvertime(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOvertime(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const otIds = row.otId || this.ids;
      this.$modal.confirm('是否确认删除加班管理编号为"' + otIds + '"的数据项？').then(function () {
        //return delOvertime(otIds);
      }).then(() => {
        //this.getList();
        //this.$modal.msgSuccess("删除成功");
        //删除逻辑
        let res = delOvertime(otIds);
        res.then((res) => {
          if (res) {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          } else {
            this.$modal.msgError("未达到结束时间，删除失败");
          }
        })
      }).catch(() => {
      });
    },
    // handleDelete(row) {
    //   const otIds = row.otId || this.ids;
    //   this.$modal.confirm('是否确认删除加班管理编号为"' + otIds + '"的数据项？').then(function() {
    //     return delOvertime(otIds);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/overtime/export', {
        ...this.queryParams
      }, `overtime_${new Date().getTime()}.xlsx`)
    }
  },

  mounted() {
    // 发送查询所有员工的请求
    listEmployee().then(response => {
      this.employee = response.rows;
      this.loading = false;
    });
  }
};
</script>
